package io.gbznt.modules.buss.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.gbznt.common.utils.Constant;
import io.gbznt.common.utils.ShiroUtils;
import io.gbznt.modules.sys.entity.SysDeptEntity;
import io.gbznt.modules.sys.service.SysDeptService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

import io.gbznt.common.utils.PageUtils;
import io.gbznt.common.utils.Query;

import io.gbznt.modules.buss.dao.BussRtuWarningRoleDao;
import io.gbznt.modules.buss.entity.BussFarmlandInfoEntity;
import io.gbznt.modules.buss.entity.BussRtuWarningRoleEntity;
import io.gbznt.modules.buss.service.BussFarmlandInfoService;
import io.gbznt.modules.buss.service.BussRtuWarningRoleService;
import io.gbznt.modules.sys.entity.SysDictEntity;
import io.gbznt.modules.sys.service.SysDictService;

@Service("bussRtuWarningRoleService")
public class BussRtuWarningRoleServiceImpl extends ServiceImpl<BussRtuWarningRoleDao, BussRtuWarningRoleEntity>
		implements BussRtuWarningRoleService {

	@Autowired
	private BussFarmlandInfoService bfinfo;
	@Autowired
	private SysDictService sysDictService;
	@Autowired
	private SysDeptService sysDeptService;

	@Override
	public PageUtils queryPage(Map<String, Object> params) {
		// 搜索条件----名称
		String mc = (String) params.get("name");
		String gjlx = (String) params.get("gjlx");

		IPage<BussRtuWarningRoleEntity> page = this.page(new Query<BussRtuWarningRoleEntity>().getPage(params),
				new QueryWrapper<BussRtuWarningRoleEntity>().eq(StringUtils.isNotBlank(gjlx), "warning_lev", gjlx)
						.like(StringUtils.isNotBlank(mc), "name", mc)
						// 个人数据权限控制
						.in(!Constant.SUPER_ADMIN.equals(ShiroUtils.getUserId()),
								"dept_id", sysDeptService.getPermissionDeptIdsArr(true,true))
						.orderByDesc("create_time")

		);
		for (BussRtuWarningRoleEntity br : page.getRecords()) {

			// 基地信息
			SysDeptEntity deptEntity = sysDeptService.getById(br.getDeptId());
			if(null!= deptEntity){
				br.setDeptName(deptEntity.getName());
			}

			// 田块信息
			String idsname = "";
			if (br.getRelationFarmlandIds() != null && StringUtils.isNotBlank(br.getRelationFarmlandIds())) {
				List<BussFarmlandInfoEntity> brentity = bfinfo
						.list(new QueryWrapper<BussFarmlandInfoEntity>().in("id", br.getRelationFarmlandIds().split(",")));
				for (int i = 0; i < brentity.size(); i++) {
					if (i == 0) {
						idsname = idsname + brentity.get(i).getName();
					} else {
						idsname = idsname + "," + brentity.get(i).getName();
					}

				}
			}
			br.setRelationFarmlandIdsname(idsname);

			// 告警级别字典
			if (StringUtils.isNotBlank(br.getWarningLev())) {
				SysDictEntity dict = sysDictService.getById(br.getWarningLev());
				if (null!=dict && StringUtils.isNotBlank(dict.getValue())) {
					br.setWarningLevValue(dict.getValue());
				}
			}

		}

		return new PageUtils(page);
	}

	@Override
	public Map<String, Object> geidict(String params) {
		// TODO Auto-generated method stub
		Map<String, Object> nm = baseMapper.getdict(params);
		return nm;
	}

}
